<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 06.12.11
 * Time: 23:52
 * To change this template use File | Settings | File Templates.
 */

class categoryController extends Core_BaseController
{

    function helloAction($message = "Hello"){

        echo ""

    }

    function indexAction($id = 1)
    {
        $auth = $this->checkAuth();
        $role = $this->getAuth()->getUserRoles($auth);
        $this->twig = $this->getRegistry()->twig;

        // TODO: Implement indexAction() method.
        $courseManager = new Course_Course();
        $categoryManager = new Category_Manager();
        $categoryManager->getAllCategories();
        $courses = $courseManager->getCoursesByCategory($id);
        $category = $categoryManager->getCategoryName($id);
        $data = array('category' => $category,
                      'categoryid' => $id,
                      'courses' => $courses,
                       'role' => $role);


        $data = array_merge($data, $auth);
        $template = $this->twig->loadTemplate('coursesInCategory.html.twig');
        echo $template->render($data);


    }

    function addAction()
    {

        $permission = array('admin', 'moderator');
        $auth = $this->checkAuth();
        $allow = $this->userHasPermission($permission);
        $this->twig = $this->getRegistry()->twig;
        if ($allow) {
             $authc = new Auth_Auth2();
            $role = $authc->getUserRoles($auth['name']);
            //var_dump($allow);
            $messages = array();

            //validate
            if(empty($_POST))
                 $messages[] = 'Введіть будь-ласка дані';
            if ($_POST['categoryName'] == '') {
                $messages[] = 'Поле `Назва категорії`не може бути пустим';
            }
            if (!is_numeric($_POST['categoryRaiting'])) {
                $messages[] = 'Поле `Рейтинг`повинне бути числовим ';
            }
            if (($raiting = $_POST['categoryRaiting']) == '')
                $raiting = null;


            //endvalidate
            if (count($messages) == 0) {

                $categoryName = $_POST['categoryName'];

                $category = new Category($raiting, $categoryName);
                $categoryManager = new Category_Manager();
                $categoryManager->addCategory($category);

                $messages = array();
                $messages[] = 'Додано';



            }
            $info = array();
            $template = $this->twig->loadTemplate('category_add1.html.twig');
            $info = array_merge($info,array('messages' => $messages,'role' => $role));
            $info = array_merge($info,$auth);
            $template->display($info);



        } else {
            $template = $this->twig->loadTemplate('access.html.twig');
            echo $template->render(array());
        }

    }

    function removeAction($id = -1)
    {
        $permission = array('admin', 'moderator');
        $auth = $this->checkAuth();
        $allow = $this->userHasPermission($permission);
        $this->twig = $this->getRegistry()->twig;
        if ($allow) {
            $categoryManager = new Category_Manager();
            $categoryManager->removeCategory($id);

            header("Location:/index");

        } else {
            $template = $this->twig->loadTemplate('access.html.twig');
            echo $template->render(array());
        }

    }

    function updateAction()
    {
      /*  $permission = array('admin', 'moderator');
        $auth = $this->checkAuth();
        $allow = $this->userHasPermission($permission);
        $this->twig = $this->getRegistry()->twig;
        if ($allow) {
            $template = $this->twig->loadTemplate('category_update.html.twig');
            $template->display(array());


        } else {
            $template = $this->twig->loadTemplate('access.html.twig');
            echo $template->render(array());
        }  */
        header("Location:http://www.google.com");

    }
}
